from django.core.paginator import Paginator
from rest_framework.views import APIView
from rest_framework.response import Response
from .sers import *


# Create your views here.

# 添加分类
class CateView(APIView):
    def post(self, request):
        # 提取网页提交数据
        name = request.data.get('name')
        # 判断分类是否存在
        name_obj = Cate.objects.filter(name=name)
        # 如何分类，返回分类已存在
        if name_obj:
            return Response({"msg": "分类已存在"}, status=400)
        # 不存在时 添加

        else:
            ser = CateSer(data=request.data)
            # 校验数据
            if ser.is_valid():
                # 保存数据
                ser.save()
                return Response({"msg": "添加成功"}, status=200)
            else:
                return Response({"error": ser.errors})

    def get(self, request):
        num = int(request.GET.get('num'))
        cate_list = Cate.objects.all()
        paginator = Paginator(cate_list, 3)
        pages = paginator.page(num)
        cate_ser = CateSer(pages, many=True)
        return Response({
            'data': cate_ser.data,
            'page_list': [i for i in paginator.page_range],
            'page_sum': paginator.num_pages
        })

    # 删除

    def delete(self, request):
        # 获取前端传来的id
        id = request.GET.get("id")
        print("id", id)
        # 删除数据
        id_cate = Cate.objects.filter(id=id)
        id_cate.delete()
        # 返回删除成功信息
        return Response({"msg": "删除成功"}, status=200)

    def put(self, request):
        id = request.GET.get("id")
        print("id", id)
        put_obj = Cate.objects.filter(id=id).update(data=request.data)
        if put_obj:
            return Response({"msg": "修改成功"}, status=200)
        return Response({"msg": "修改失败"}, status=400)

# 分类展示
class CatesView(APIView):
    # 获取分类全部数据
    def get(self, request):
        cate_list = Cate.objects.all()
        cate_ser = CateSer(cate_list, many=True)
        return Response(cate_ser.data)


# 商品添加
class GoodsView(APIView):
    def post(self, request):
        # 提取网页提交数据
        name = request.data.get('name')
        # 判断商品是否存在
        name_obj = Goods.objects.filter(name=name)
        # 如何商品，返回分类已存在
        if name_obj:
            return Response({"msg": "商品已存在"}, status=400)
            # 不存在时 添加
        else:
            ser = GoodsSer(data=request.data)
            # 校验数据
            if ser.is_valid():
                print(ser.validated_data)
                # 保存数据
                ser.save()
                return Response({"msg": "添加成功"}, status=200)
            else:
                print(ser.errors)
                return Response({"error": ser.errors})
        # 不存在时 添加
        # else:
        #     good_obj = GoodsSer(data=request.data)
        #     if good_obj.is_valid():
        #         print(good_obj.validated_data)
        #         good_obj.save()
        #         return Response({"msg": "商品添加成功"}, status=200)
        #     else:
        #         print(good_obj.errors)
        #         return Response({"msg": "商品添加失败"}, status=500)

    # 展示
    def get(self, request):
        num = int(request.GET.get('num'))
        cate_list = Goods.objects.all()
        paginator = Paginator(cate_list, 3)
        pages = paginator.page(num)
        cate_ser = GoodsSer(pages, many=True)
        return Response({
            'data': cate_ser.data,
            'page_list': [i for i in paginator.page_range],
            'page_sum': paginator.num_pages
        })

# 商品展示
class GoodsListView(APIView):
    # 获取分类全部数据
    def get(self, request):
        cate_list = Goods.objects.all()
        cate_ser = GoodsLisrSer(cate_list, many=True)
        return Response(cate_ser.data)
